package com.enjoy.mediademo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;

public class MediaRecordActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG=MediaRecordActivity.class.getSimpleName();
    private TextureView textureView;
    private Button btn_opt;
    private MediaRecorder mediaRecorder;
    private Camera camera;
    private CameraManager cameraManager;
    private String cameraId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        textureView = findViewById(R.id.textureView);
        btn_opt = findViewById(R.id.btn_opt);

        btn_opt.setOnClickListener(this);
         cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        String[] cameraIdList=null;
        try {
             cameraIdList = cameraManager.getCameraIdList();
            cameraId = cameraManager.getCameraIdList()[1];
            Log.e("TAG", "onCreate: "+cameraIdList.length);
            for (String cameraId : cameraIdList) {
                Log.e("TAG", this.cameraId+"onCreate:CameraManager-- "+cameraId);
            }
            // 遍历摄像头ID列表，找到你想要的摄像头

        String desiredCameraId = null;
        for (String cameraId : cameraIdList) {
            CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
            // 根据特性选择摄像头，例如选择后置摄像头
            Integer lensFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
            Log.d(TAG, "characteristics: "+lensFacing);
            if (lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                desiredCameraId = cameraId;
                break;
            }
        }
        if (desiredCameraId != null) {
            try {
                cameraManager.openCamera(cameraId, stateCallback, null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        CharSequence text = btn_opt.getText();
        if (TextUtils.equals(text, "开始")) {
            btn_opt.setText("结束");
            camera = Camera.open(6);
            camera.setDisplayOrientation(90);
            camera.unlock();
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setCamera(camera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置音频源 麦克风
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);//设备视频源 摄像头
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//指定视频文件格式
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mediaRecorder.setOrientationHint(90);
            //设置视频输出文件
            mediaRecorder.setOutputFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "a.mp4").getAbsolutePath());
            mediaRecorder.setVideoSize(640, 480);
            mediaRecorder.setPreviewDisplay(new Surface(textureView.getSurfaceTexture()));
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaRecorder.start();
        } else {
            btn_opt.setText("开始");
            mediaRecorder.stop();
            mediaRecorder.release();
            camera.stopPreview();
            camera.release();
        }

    }
   final static CameraDevice.StateCallback stateCallback=new  CameraDevice.StateCallback(){
       @Override
       public void onOpened(@NonNull CameraDevice camera) {
           Log.e(TAG, "onOpened: ");
       }

       @Override
       public void onDisconnected(@NonNull CameraDevice camera) {
           Log.e(TAG, "onDisconnected: ");
       }

       @Override
       public void onError(@NonNull CameraDevice camera, int error) {
           Log.e(TAG, "onError: " );
       }
   };
}
